// pages/vote/toDoTitle.js
const app = getApp()
import {
  urls
} from '../../api/urls'
import {
  item_list_query, //投票题目目录查询
  select_list_query, //被选答案查询 
  answer_commit, //选择答案更新提交
} from '../../api/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: [],
    Dicide_id: '',
    item_list: [],
    dicide_list: {},
    item: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      Dicide_id: options.dicide_id,
      dicide_list: JSON.parse(options.item),
      item: options.item,
      globalData: app.globalData
    })
    if (options.cur_property_code) { //研判从分享链接过来
      app.globalData.cur_property_code = options.cur_property_code
    }

    this.item_list_query(this.data.Dicide_id)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},


  //单选题 选择
  onChangeRadio(event) {
    console.log(event.currentTarget.dataset.index, event.currentTarget.dataset)
    var index = event.currentTarget.dataset.index
    var radio = this.data.radio
    var item_id = event.currentTarget.dataset.item_id
    radio[index][0] = event.detail
    this.setData({
      radio
    });
    this.answer_commit(item_id, event.detail.toString())
  },

  //多选题
  onChangeCheckbox(event) {
    var index = event.currentTarget.dataset.index
    var radio = this.data.radio
    var maxTm = event.currentTarget.dataset.itemnum
    var item_id = event.currentTarget.dataset.item_id
    if (event.detail.length > parseInt(maxTm)) {
      wx.showModal({
        title: '提示',
        confirmText: "确定",
        showCancel: false, //是否显示取消按钮
        content: '本选项答案不能同时超过【' + parseInt(maxTm) + '】个',
      })
      return false //答题数目控制
    }
    radio[index] = event.detail
    this.setData({
      radio
    });
    this.answer_commit(item_id, event.detail.toString()) //将数组转换为字符串 更新答题
  },

  submit_click() {
    wx.showModal({
      title: '投票或民调',
      confirmText: "确定",
      showCancel: false, //是否显示取消按钮
      content: '你的选择已提交成功，谢谢！',
      success(res) {
        wx.navigateBack({})
      }
    })
    console.log('-------提交成功------');
  },

  //选择答案更新提交
  answer_commit(item_id, select_id) {
    if (app.globalData.cur_my_openid == '') {
      wx.showToast({
        title: '只能查看',
        content: '投票请通过小程序进入...'
      })
      return
    }

    var data = {
      para: [app.globalData.cur_my_openid,
        app.globalData.cur_property_code,
        app.globalData.cur_room_code,
        this.data.Dicide_id,
        item_id,
        select_id,
        '1', //微信小程序渠道
        'openid=' + app.globalData.cur_my_openid + ' nikename=' + app.globalData.cur_my_name
      ],
    }

    answer_commit(data).then(answer_res => {
      console.log('提交成功!')
    })
  },

  //查询题目
  item_list_query(dicide_id) {
    const _this = this
    var item_select = _this.data.item_select
    var radio = _this.data.radio

    var data = {
      para: [dicide_id],
    }
    item_list_query(data).then(res => {
      var item_list = res.data

      res.data.forEach((val, index, arr) => {
        radio.push([])

        var data = {
          para: [app.globalData.cur_my_openid, app.globalData.cur_property_code, app.globalData.cur_room_code, dicide_id, val.ITEM_ID],
        }
        select_list_query(data).then(answer_res => {

          item_list[index].answer_list = answer_res.data //添加答案清单
          answer_res.data.forEach(res_select => {


            if (res_select.SELECT_RET == '1') {
              radio[index].push(res_select.SELECT_ID)
            }

          }) // item_select end

          _this.setData({
            item_list,
            radio,
            item_select
          })

        })
      })


    }) //item_list end
  },


  //展开更多
  info_more: function (e) {
    console.log('------------ item_list----', e)
    var that = this;
    var item_index = e.currentTarget.dataset.item_index;
    var answer_index = e.currentTarget.dataset.answer_index;
    for (var i = 0; i < that.data.item_list.length; i++) {
      if (item_index == i) {
        that.data.item_list[item_index].answer_list[answer_index].AUTO = 'true';
        that.data.item_list[item_index].answer_list[answer_index].SEEMORE = 'false';
      }
    }
    that.setData({
      item_list: that.data.item_list
    })
  },


  //内容收起
  info_close: function (e) {
    var that = this;
    var item_index = e.currentTarget.dataset.item_index;
    var answer_index = e.currentTarget.dataset.answer_index;
    for (var i = 0; i < that.data.item_list.length; i++) {
      if (item_index == i) {
        that.data.item_list[item_index].answer_list[answer_index].AUTO = 'true';
        that.data.item_list[item_index].answer_list[answer_index].SEEMORE = 'true';
      }
    }
    that.setData({
      item_list: that.data.item_list
    })
  },

  //分享朋友圈
  onShareTimeline: function () {
    return {
      title: app.globalData.signature_name+app.globalData.cur_property_name + this.data.dicide_list.DICIDE_TITLE,
      query: 'dicide_id=' + this.data.Dicide_id + '&item=' + this.data.item + '&cur_property_code=' + app.globalData.cur_property_code,
      imageUrl: 'https://yz.2alive.cn/image/logo.png',
    }
  },


 
  // 用户点击右上角分享
  onShareAppMessage: function () {
    return {
      title: app.globalData.signature_name+app.globalData.cur_property_name + this.data.dicide_list.DICIDE_TITLE,
      path: '/pages/vote/toDoTitle?dicide_id=' + this.data.Dicide_id + '&item=' + this.data.item + '&cur_property_code=' + app.globalData.cur_property_code,
      imageUrl: app.globalData.share_img_url,
    }
  },

})